phpDocumentor Web Commons
[ class tree: Web Commons ] [ index: Web Commons ] [ all elements ]

Source for file persistence.php

Documentation is available at persistence.php

  1. <?php
  2. /**
  3.  * This file groups classes pertaining to data persistence.
  4.  * 
  5.  * @author Antoine d'Otreppe de Bouvette <a.dotreppe@aspyct.org>
  6.  * @license http://www.opensource.org/licenses/mit-license.php
  7.  * @version 0.1dev
  8.  * 
  9.  * @todo Make usage of "query" and "statement" more clear.
  10.  * @todo Write a DAO that maps to entities.
  11.  */
  12.  
  13. /**
  14.  * Offers connectivity to databases.
  15.  * @since 0.1
  16.  */
  17. class DataSource extends PDO {
  18.     /**
  19.      * Refer to {@link http://www.php.net/manual/pdo.construct.php PDO} for
  20.      * details about this constructor.
  21.      * @param string $dsn 
  22.      * @param string $username 
  23.      * @param string $passwd 
  24.      * @param array $driver_options 
  25.      */
  26.     public function __construct($dsn$username=Null$passwd=Null,
  27.                                 $driver_options=array()) {
  28.         parent::__construct($dsn$username$passwd$driver_options);
  29.         $this->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
  30.     }
  31. }
  32.  
  33. /**
  34.  * Serves as a basis for a PDO DAO.
  35.  * @since 0.1
  36.  */
  37. abstract class AbstractDAO {
  38.     /**
  39.      * The PDO connection
  40.      * @var PDO 
  41.      */
  42.     private $ds;
  43.     
  44.     public function setDataSource(DataSource $ds{
  45.         $this->ds $ds;
  46.     }
  47.     
  48.     /**
  49.      * Prepares and executes a SQL $statement within the connection
  50.      * 
  51.      * @param string $statement 
  52.      * @param mixed[] $args 
  53.      * @return PDOStatement 
  54.      * @since 0.1
  55.      */
  56.     protected function execute($statement$args=array()) {
  57.         $ps $this->ds->prepare($statement);
  58.         $ps->execute($args);
  59.         
  60.         return $ps;
  61.     }
  62.     
  63.     /**
  64.      * Executes an insert statement and returns the last insert id.
  65.      * 
  66.      * @todo Make it more transparent for PostgreSQL: it might guess serial name
  67.      * 
  68.      * @param string $statement 
  69.      * @param arrray $args 
  70.      * @return int the last insert id for the data source
  71.      * @since 0.1
  72.      */
  73.     protected function insertAndReturnId($statement$args$serial=Null{
  74.         $this->execute($statement$args);
  75.         return $this->ds->lastInsertId($serial);
  76.     }
  77.     
  78.     /**
  79.      * Executes a statement and maps the resulting rows with
  80.      * {@link AbstractDAO::hydrate()}.
  81.      * 
  82.      * @param string $statement 
  83.      * @param array $args 
  84.      * @return array 
  85.      * @since 0.1
  86.      */
  87.     protected function select($statement$args=array()) {
  88.         $statement $this->execute($statement$args);
  89.         $cb array($this'hydrate');
  90.         return array_map($cb$statement->fetchAll(PDO::FETCH_ASSOC));
  91.     }
  92.     
  93.     /**
  94.      * Interprets row data.
  95.      * This default implementation returns the row itself.
  96.      * Override this method to add custom behaviour.
  97.      * 
  98.      * @param array $row an associative array containing row values.
  99.      * @return Entity 
  100.      * @since 0.1
  101.      */
  102.     protected function hydrate($row{
  103.         return $row;
  104.     }
  105. }
  106.  
  107. /**
  108.  * Exception thrown when there is no more row to build
  109.  * @since 0.1
  110.  */
  111. class NoMoreRowException extends Exception {}

Documentation generated on Fri, 16 Jul 2010 00:48:39 +0200 by phpDocumentor 1.4.3